package ReflectClassTwo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.channels.ClosedSelectorException;

public class ReflectClassUse {
    //创建对象，获得学生对象
    public static void reflectNewInstance() throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Class<?> c1 = Class.forName("ReflectClassTwo.Student");

        //newInstance创建类的实例
        Student student = (Student) c1.newInstance();
        System.out.println(student);
    }

    //反射私有构造方法
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("ReflectClassTwo.Student");

            Constructor<?> constructor = c1.getDeclaredConstructor(String.class,int.class);
            //因为constructor是私有的，所以我们需要把他设置成true,使用setAccessible方法后就可以修改私有对象了
            constructor.setAccessible(true);
            Student student = (Student) constructor.newInstance("ymm",18);

            System.out.println(student);


        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //反射私有属性
    public static void reflectPrivateField() {
        //调用name，name在Student中是private
        try {
            Class<?> c1 = Class.forName("ReflectClassTwo.Student");

            Student student = (Student) c1.newInstance();
            //获取私有属性
            Field field = c1.getDeclaredField("name");
            //因为是name私有的属性，所以要setAccessible(true)；
            field.setAccessible(true);
            //开始修改
            field.set(student,"yes");


            System.out.println(student);


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }
    }

    //反射私有方法
    public static void reflectPrivateMethod() {
        try {
            //获取一个Class对象
            Class<?> c1 = Class.forName("ReflectClassTwo.Student");
            //通过Class对象实例化一个学生对象
            Student student = (Student) c1.newInstance();

            //通过学生对象获取getDeclaredMethod方法,双引号中间是你要获取哪一个方法
            //function是有参数的，可以在方法后接着传入参数类型
            Method method = c1.getDeclaredMethod("function",String.class);

            //想要私有的方法被修改需要setAccessible(true)
            method.setAccessible(true);

            //此时说明这个方法可以被调用了
            //调用student这个对象，这个方法有一个String类型的参数
            method.invoke(student,"我是一个参数");


            System.out.println(student);


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        reflectPrivateMethod();
    }
    public static void main3(String[] args) {
        reflectPrivateField();//name = 'yes' ,age = 18
    }

    public static void main2(String[] args) {
        reflectPrivateConstructor(); // name = 'ymm',age = 18;
    }

    //通过反射来创建对象
    public static void main1(String[] args) {
        try {
            reflectNewInstance(); //name = 'yxj', age = 18;
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

}
